<?php
namespace App\Services\Enterprise;
use App\Model\Resume;
use App\Services\CommonService;
use App\Model\Sendlist;
use App\Model\Invite;
use App\Model\User;
use Illuminate\Support\Facades\DB;
use App\Model\Position;
class ResumeService extends CommonService{
    /**
     * 简历管理
     */
    public function ResumeLists($id){
        $data=Resume::join('re_sendlist as s','s.resumid','=','re_resume.resume_id')
            ->join('re_user as u','u.id','=','s.uid')
            ->where('s.jobid',$id)
            ->get(['s.id','u_head_picture','u_name','u_sex','u_school','u_major','u_education','resume_work','ctime'])
            ->toArray();
        return $this->success($data);
    }

    /**
     * 带筛选
     * @param $id
     */
    public function ResumeList($id){
        $data=Resume::join('re_sendlist as s','s.resumid','=','re_resume.resume_id')
            ->join('re_user as u','u.id','=','s.uid')
            ->where('s.jobid',$id)
            ->where('s.status',1)
            ->get(['s.id','u_head_picture','u_name','u_sex','u_school','u_major','u_education','resume_work','ctime'])
            ->toArray();
        return $this->success($data);
    }
    /**
     * 被拒绝的简历列表
     */
    public function ResumeNolist($id){
        $data=Resume::join('re_sendlist as s','s.resumid','=','re_resume.resume_id')
            ->join('re_user as u','u.id','=','s.uid')
            ->where('s.jobid',$id)
            ->where('s.status',3)
            ->get(['s.id','u_head_picture','u_name','u_sex','u_school','u_major','u_education','resume_work'])
            ->toArray();
        return $this->success($data);
    }
    /**
     * 简历批量   修改不合适
     */
    public function ResumeDel($ids,$connect){
        if(is_numeric($ids)){
            //单一删除
            $resule=Sendlist::where('id',$ids)->update(['status'=>3,'connect'=>$connect]);
        }else{
            //批量删除
            $resule=Sendlist::whereIn('id',$ids)->update(['status'=>3,'connect'=>$connect]);
        }
        if($resule){
            return $this->success();
        }else{
            return $this->fail('4003');
        }
    }
    /**
     * 简历管理  邀约面试
     */
    public function InviteAdd($data){
        $ids=$data['ids'];
        unset($data['ids']);
        DB::transaction(function () use($ids,$data){
            if(is_numeric($ids)){
                //单一邀约  修改类型
                $resule=Sendlist::where('id',$ids)->update(['status'=>2]);
                //查出面试的用户id
                $uid=Sendlist::Find($ids)->toArray()['uid'];
                //查出面试用户的联系电话
                $data['linkman']=User::find($uid)->toArray()['u_phone'];
                //添加数据
                Invite::insert($data);
            }
        });
        return $this->success();

    }
    /**
     * 查询该公司所有职位
     */
    public function PositionList($id){
        $arr=Sendlist::join("re_position as p",'p.position_id','=','re_sendlist.jobid')
            ->where('p.company_id',$id)
            ->groupBy('p.position_id')
            ->get(['p.position_name'])->toArray();
        return $this->success($arr);
    }
    /**
     * 查询该公司简历的所有学校
     */
    public function SchoolList($id){
        $arr=Sendlist::join("re_position as p",'p.position_id','=','re_sendlist.jobid')
            ->join('re_user as u','u.id','=','re_sendlist.uid')
            ->where('p.company_id',$id)
            ->groupBy('u.id')
            ->get(['u_school'])->toArray();
        return $this->success($arr);
    }
    /**
     * 查询该公司简历的所有学校
     */
    public function MajorList($id){
        $arr=Sendlist::join("re_position as p",'p.position_id','=','re_sendlist.jobid')
            ->join('re_user as u','u.id','=','re_sendlist.uid')
            ->where('p.company_id',$id)
            ->groupBy('u.id')
            ->get(['u_major'])->toArray();
        return $this->success($arr);
    }
}